package com.ly.hotel.view.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ly.hotel.model.Ad;
import com.ly.hotel.model.AdDetail;
import com.ly.hotel.msgevent.CloudAdShowFinishEvent;
import com.ly.hotel.msgevent.CloudSetAdCloseEvent;
import com.ly.hotel.util.ImageUtil;

import de.greenrobot.event.EventBus;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class MyAdImageView extends ImageView {

	private Context context;
	MyHandler mHandler = new MyHandler();
	private Ad ad = null;
	public int x = 0, y = 0;// 广告框的起始位置
	public int height = 0, width = 0;
	public boolean closeEnable = true;
	public int layoutId = 0;

	public MyAdImageView(Context context) {
		super(context);
		this.context = context;
		Log.i("AdShowActivity", "MyAdImageView(Context context)");
		init();
	}

	public MyAdImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		Log.i("AdShowActivity", "MyAdImageView(Context context, AttributeSet attrs,int defStyle)");
		init();
	}

	public MyAdImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		Log.i("AdShowActivity", "MyAdImageView(Context context, AttributeSet attrs)");
		init();
	}

	public void init() {
		if (AdData.getTv() != null && !AdData.getTv().getDetails().isEmpty()) {
			for (AdDetail adl : AdData.getTv().getDetails()) {
				if (adl.getType() == AdDetail.PIC) {
					// setAd(AdData.getTv());
					ad = AdData.getTv();
					break;
				}
			}
		}
		
		setAdImg();
	}

	public void setXY(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public void setAdImg() {
		// 图片
		if(ad == null || ad.getDetails() == null || ad.getDetails().isEmpty()){
			Log.i("AdShowActivity", "没有广告，退出之.");
			setVisibility(View.GONE);
			return;
		}

		new Thread(new Runnable() {

			@Override
			public void run() {
				final List<Bitmap> imgs = new ArrayList<Bitmap>();
				final Map<Bitmap, Integer> imagesIntervalMap = new HashMap<Bitmap, Integer>();
				// 加载图片
				for (AdDetail detail : ad.getDetails()) {
					if (detail.getType() == AdDetail.PIC && detail.isEnable() && !detail.localPath.equals("")
							&& detail.isCache) {
						Log.i("AdShowActivity", "加载图片,url=" + detail.localPath);
						Bitmap loadImage = ImageUtil.loadImage(context, detail.localPath);
						if (loadImage != null) {
							Log.i("AdShowActivity", "显示时间=" + detail.getSwitchTime());
							imgs.add(loadImage);
							imagesIntervalMap.put(loadImage, detail.getSwitchTime() * 1000);
						} else {
							Log.i("AdShowActivity", "加载图片失败..." + detail.getPath());
						}
					}
				}
				if (imgs.isEmpty()) {
					return;
				}
				Log.i("AdShowActivity", "轮播图片加载完成.");
				// 时间到就进入主页面
				mHandler.postDelayed(new Runnable() {
					public void run() {
						MyAnimationDrawable ani = new MyAnimationDrawable() {
							@Override
							public void onFrameEnd(int next) {
								EventBus.getDefault().post(
										new CloudAdShowFinishEvent(ad.getDetails().get(next).getId()));
							}
						};

						for (Bitmap bm : imgs) {
							Log.i("AdShowActivity", "imagesIntervalMap.get(bm)=" + imagesIntervalMap.get(bm));
							ani.addFrame(new BitmapDrawable(context.getResources(), bm), imagesIntervalMap.get(bm));
						}
						ani.setOneShot(true);
						setImageDrawable(ani);
						Log.i("AdShowActivity", "开始播放广告.");
						ani.start();
						// progressDialog.cancel();
						// 计算总共显示的时间
						int duration = 0;
						for (int i = 0; i < ani.getNumberOfFrames(); i++) {
							Log.i("AdShowActivity", "ani.getDuration=" + ani.getDuration(i));
							duration += ani.getDuration(i);
						}

						// 时间到就进入主页面
						mHandler.sendEmptyMessageDelayed(0, duration);
						EventBus.getDefault().post(new CloudSetAdCloseEvent(duration));
					}
				}, 100);
			}
		}).start();

	}

	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				setVisibility(View.GONE);
				break;
			}
		}
	};
}
